package com.laiketui.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laiketui.order.api.enums.OrderParentStatusEnum;
import com.laiketui.order.api.enums.OrderStatusEnum;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderParent;
import com.laiketui.order.service.OrderService;
import com.laiketui.order.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author jiangxiang
* @description 针对表【lk_order(订单表)】的数据库操作Service实现
* @createDate 2024-08-15 23:56:36
*/
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Override
    public List<Order> findByOrderParentNo(String orderParentNo) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>();
        wrapper.eq(Order::getOrderParentNo, orderParentNo);
        return list(wrapper);
    }

    @Override
    public boolean updateStatusCancel(String orderParentNo) {
        log.info("开始取消子单:{}",orderParentNo);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderParentNo, orderParentNo);
        wrapper.eq(Order::getStatus, OrderStatusEnum.WAIT_PAY.getCode());
        Order order = new Order();
        order.setUpdateBy("系统取消订单");
        order.setUpdateTime(new Date());
        order.setStatus(OrderStatusEnum.CANCEL.getCode());
        boolean update = update(order, wrapper);
        log.info("取消子单:{}",update);
        return update;
    }

    @Override
    public Order findByOrderNo(String orderNo) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>();
        wrapper.eq(Order::getOrderNo, orderNo);
        return getOne(wrapper);
    }

    @Override
    public boolean updateByOrderNo(Order order, String orderNo) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>();
        wrapper.eq(Order::getOrderNo, orderNo);
        return update(order, wrapper);
    }

    @Override
    public boolean updateStatusFinish(String orderNo,String userId) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderNo, orderNo);
        wrapper.eq(Order::getStatus, OrderStatusEnum.WAIT_RECEIVE.getCode());
        Order order = new Order();
        order.setUpdateBy(userId);
        Date now = new Date();
        order.setUpdateTime(now);
        order.setStatus(OrderStatusEnum.FINISH.getCode());
        order.setRealDeliveryTime(now);
        return update(order, wrapper);
    }

    @Override
    public boolean updateByOrderParentNo(String orderParentNo, Order order) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderParentNo, orderParentNo);
        return update(order, wrapper);
    }
}




